package com.emm.yixun.system.service.impl.notice.deal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.CustomerService;
import com.emm.yixun.basic.service.DictionaryService;
import com.emm.yixun.basic.service.PriceSchemeService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.basic.service.ProjectService;
import com.emm.yixun.basic.service.ProjectSettingService;
import com.emm.yixun.basic.service.ReserveAreaService;
import com.emm.yixun.basic.service.SignedService;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.basic.service.TransactionRelationService;
import com.emm.yixun.basic.service.UserService;
import com.emm.yixun.common.model.Dictionary;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.ReserveArea;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.DictionaryDto;
import com.emm.yixun.common.model.dto.NoticeEntity;
import com.emm.yixun.common.model.dto.ReserveAreaDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.utils.MsgUtil;
import com.emm.yixun.system.service.impl.manage.SysNoticeManage;
import com.emm.yixun.system.service.notice.deal.ReserveAreaNoticeFacade;

/**
 * 价格方案审批消息
 * @author jhsong
 *
 */
@Service("reserveAreaNoticeFacade")
@WebService(name="com.emm.yixun.system.service.ReserveAreaNoticeFacade")
public class ReserveAreaNoticeFacadeImpl implements ReserveAreaNoticeFacade{
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserService userService;
	@Autowired
	private TransactionRelationService transactionRelationService;
	@Autowired
	private ProjectSettingService projectSettingService;
	@Autowired
	private ProjectRoomService projectRoomService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private SysNoticeManage sysNoticeManage;
	@Autowired
	private PriceSchemeService priceSchemeService;
	@Autowired
	private SignedService signedService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private ReserveAreaService reserveAreaService;
	
	@Override
	public Response<Boolean> createReserveAreaNotice(NoticeEntity noticeEntity) {
		Response<Boolean> response = new Response<Boolean>();
		try {
			// 实体对象
			Signed signed_mjbc = signedService.selectById(noticeEntity.getNoticeBusinessId());
			// 经纪人
			/*TransactionRelationDto transactionRelationDto = new TransactionRelationDto();
			transactionRelationDto.setBusinessId(signed_mjbc.getId());
			List<TransactionRelation> transactionRelationList = transactionRelationService.selectListByDto(transactionRelationDto);
			List<User> userList = new ArrayList<User>();
			if (transactionRelationList.size() > 0) {
				for (int i = 0; i < transactionRelationList.size(); i++) {
					User user = userService.selectById(transactionRelationList.get(i).getRelationAgentid());
					userList.add(user);
				}
			}*/
			// 发起人
			List<User> userList = new ArrayList<User>();
			User user_fqr = userService.selectById(signed_mjbc.getCreateUserId());
			userList.add(user_fqr);
			// 房源
			String proName = "";
			if (signed_mjbc.getRoomId() != null && !"".equals(signed_mjbc.getRoomId())) {
				ProjectRoom project = projectRoomService.selectById(signed_mjbc.getRoomId());
				if (project != null) {
					proName = project.getCombinedName();
				}
			}
			//逆向流程，从业务异常表获取审核状态
			ReserveAreaDto reserveAreaDto = new ReserveAreaDto();
			reserveAreaDto.setSignedId(signed_mjbc.getId());
			ReserveArea reserveArea = reserveAreaService.selectByDto(reserveAreaDto);
			// 项目
			/*ProjectDto proDto = new ProjectDto();
			proDto.setProjectCode(signed_mjbc.getProjectCode());
			Project project = projectService.selectByDto(proDto);*/

			// 面积补差流程, 获取字典表dictionary_value = 'NT37' 的消息模板
			// 消息模板: 面积补差流程：【${auditStatus}】房号：${roomName}
			String msgTemp = "面积补差流程审核结果【${auditStatus}】房号：${roomName}";
			String sendMsg = "";
			DictionaryDto dicDto = new DictionaryDto();
			dicDto.setDictionaryValue("NT37");
			Dictionary dic = dictionaryService.selectByDto(dicDto);
			if (dic != null) {
				msgTemp = dic.getDictionaryName();
			}
			sendMsg = msgTemp.replace("${auditStatus}",
					AuditStatusEnum.getByNo(reserveArea.getAuditStatus()).getDes()).replace("${roomName}", proName);
			// 创建提醒消息及消息发送人信息
			Map<String, String> noticeParamMap = new HashMap<String, String>();
			noticeParamMap.put("noticeDicId", dic.getId()+"");
			noticeParamMap.put("noticeTempType", "NT37");
			noticeParamMap.put("sendMsg", sendMsg);
			noticeParamMap.put("title", "面积补差流程审核结果");
			noticeParamMap.put("merchantId", String.valueOf(signed_mjbc.getMerchantId()));
			noticeParamMap.put("userCode", signed_mjbc.getUserCode());
			noticeParamMap.put("accessCode", signed_mjbc.getAccessCode());
			noticeParamMap.put("createUserId", String.valueOf(signed_mjbc.getCreateUserId()));
			noticeParamMap.put("projectCode", signed_mjbc.getProjectCode());
			noticeParamMap.put("msgOrder", "1");
			/*
			 * (ym) 提醒内容增加超链接 start
			 * */
			Map<String,String> replaceValuesMap=new HashMap<String,String>();
			replaceValuesMap.put(MsgUtil.ROOM_ID, String.valueOf(signed_mjbc.getRoomId()));
			replaceValuesMap.put(MsgUtil.ROOM_NAME, proName);
			replaceValuesMap.put(MsgUtil.CUSTOMER_ID, "");
			replaceValuesMap.put(MsgUtil.CUSTOMER_NAME,"");
			replaceValuesMap.put(MsgUtil.PRICE_SCHEME_ID, "");
			replaceValuesMap.put(MsgUtil.PRICE_SCHEME_NAME, "");
			replaceValuesMap.put(MsgUtil.BUSINESS_ID, signed_mjbc.getId()+"");
			replaceValuesMap.put(MsgUtil.NEXT_NODE_ID, "");
			replaceValuesMap.put(MsgUtil.PROCESS_INSTANCE_ID, signed_mjbc.getProcInstId()+"");
	    	noticeParamMap.put("replaceValues", MsgUtil.getReplaceValues(replaceValuesMap));

	    	/*
			 * (ym) 提醒内容增加超链接 end
			 * */
	    	sysNoticeManage.createNoticeWithNoticeTemp(noticeParamMap, userList);

	    	response.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			response.setSuccess(false);
			logger.error("组装价格方案消息异常:{}",e);
		}
		return response;
	}
	
}
